package com.ebt.m.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ebt.m.AppContext;
import com.ebt.m.R;
import com.ebt.m.commons.utils.ToastUtil;
import com.ebt.m.data.bean.CardInfo;
import com.ebt.m.utils.TimeUtils;
import com.ebt.m.view.CircleImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 用户绑定信息显示页面
 * 如果用户的 phone 不为空，则显示 phone；为空，则网络获取 UserInfo 中的 phone
 */
public class MineInfoActivity extends BaseToolBarActivity {


    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.user_sex)
    TextView userSex;
    @BindView(R.id.user_company)
    TextView userCompany;
    @BindView(R.id.user_employee)
    TextView userEmployee;
    @BindView(R.id.user_phone)
    TextView userPhone;
    @BindView(R.id.user_license)
    TextView userLicense;
    @BindView(R.id.user_start)
    TextView userStart;
    @BindView(R.id.user_end)
    TextView userEnd;
    @BindView(R.id.tv_end_tips)
    TextView tvEndTips;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_info);
        ButterKnife.bind(this);
        setTitle("个人资料");
    }

    @Override
    protected void init() {
        super.init();
        initViews();
    }

    public void initViews() {
        CardInfo cardInfo = AppContext.getCurrentUser().getCardInfo();
        if (cardInfo != null) {
            userName.setText(cardInfo.getName());
            userCompany.setText(cardInfo.getCompanyName());
            userSex.setText(cardInfo.getSexValue());
            userEmployee.setText(cardInfo.getEmployeeNo());
            userPhone.setText(cardInfo.getPhone());
            String licenceNo = cardInfo.getLicenseNo();
            userLicense.setText(TextUtils.isEmpty(licenceNo) ? "-" : licenceNo);
            userStart.setText(TimeUtils.getDateStrByLong("yyyy/MM/dd", cardInfo.getValStartDate()));
            userEnd.setText(TimeUtils.getDateStrByLong("yyyy/MM/dd", cardInfo.getValEndDate()));
            String dateEndTip = calDateEndTip(cardInfo.getValEndDate());
            if (!TextUtils.isEmpty(dateEndTip)) {
                tvEndTips.setVisibility(View.VISIBLE);
                tvEndTips.setText(dateEndTip);
            } else {
                tvEndTips.setVisibility(View.GONE);
            }
        } else {
            ToastUtil.show(this, "个人资料为空");
        }
    }

    private String calDateEndTip(long valEndDate) {
        if (valEndDate == 0.0) return null;
        long deltaDays = (valEndDate - AppContext.getCurrentTime()) / 1000 / 60 / 60 / 24;
        if (deltaDays >= 0 && deltaDays < 30) {
            return "(即将过期)";
        } else if (deltaDays < 0) {
            return "(已过期)";
        }
        return null;
    }
}
